package com.example.p2p.fragment;

import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.p2p.R;
import com.example.p2p.bean.Image;
import com.example.p2p.bean.Index;
import com.example.p2p.bean.Product;
import com.example.p2p.common.AppNetConfig;
import com.example.p2p.common.BaseFragment;
import com.example.p2p.ui.RoundProgress;
import com.loopj.android.http.RequestParams;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.util.BannerUtils;

import java.util.List;

import butterknife.Bind;


public class HomeFragment extends BaseFragment {


    @Bind(R.id.iv_title_back)
    ImageView ivTitleBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_setting)
    ImageView ivTitleSetting;

    @Bind(R.id.tv_home_product)
    TextView tvHomeProduct;
    @Bind(R.id.tv_home_yearrate)
    TextView tvHomeYearrate;
    @Bind(R.id.banner)
    Banner banner;
    @Bind(R.id.roundPro_home)
    RoundProgress roundProHome;

    private Index index;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            roundProHome.setMax(100);
            for (int i = 0; i < currentProress; i++) {
                roundProHome.setProgress(i + 1);

                SystemClock.sleep(20);
                //强制重绘
//                roundProHome.invalidate();//只有主线程才可以如此调用
                roundProHome.postInvalidate();//主线程、分线程都可以如此调用
            }
        }
    };
    private int currentProress;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return AppNetConfig.INDEX;
    }

    @Override
    protected void initData(String content) {
        if (TextUtils.isEmpty(content)){//数据为空
            return;
        }
        index = new Index();
        //封装转json数据
        JSONObject jsonObject = JSON.parseObject(content);
        //解析数据（根据key获取数据）
        //1.json对象数据
        String proInfo = jsonObject.getString("proInfo");
        Product product = JSON.parseObject(proInfo, Product.class);

        //2.json数据数据
        String imagearr = jsonObject.getString("imageArr");
        List<Image> images = JSON.parseArray(imagearr, Image.class);
        //封装数据进Index类里
        index.images = images;
        index.product = product;
        //更新页面数据
        tvHomeProduct.setText(product.name);
        tvHomeYearrate.setText(product.yearRate);
        //获取数据中的进度值
        currentProress = Integer.parseInt(index.product.progress);

        //在分线程中，实现进度的动态变化
        new Thread(runnable).start();


        banner.setAdapter(new BannerImageAdapter<Image>(images) {
            @Override
            public void onBindView(BannerImageHolder holder, Image image, int position, int size) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                        .load(image.IMAURL)
                        .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        });
        banner.setIndicator(new RectangleIndicator(getContext()));
//                banner.setIndicator(new RoundLinesIndicator(getContext()));
        banner.setIndicatorSelectedWidth((int) BannerUtils.dp2px(12));
//        AsyncHttpClient client = new AsyncHttpClient();
//        client.post("http://114.55.249.135:8888/home_message", new AsyncHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                //封装转json数据
//                JSONObject jsonObject = JSON.parseObject(new String(responseBody));
//                //解析数据（根据key获取数据）
//                //1.json对象数据
//                String proInfo = jsonObject.getString("proInfo");
//                Product product = JSON.parseObject(proInfo, Product.class);
//
//                //2.json数据数据
//                String imagearr = jsonObject.getString("imageArr");
//                List<Image> images = JSON.parseArray(imagearr, Image.class);
//                //封装数据进Index类里
//                index.images = images;
//                index.product = product;
//
//                //更新页面数据
//                tvHomeProduct.setText(product.name);
//                tvHomeYearrate.setText(product.yearRate);
//                //获取数据中的进度值
//                currentProress = Integer.parseInt(index.product.progress);
//
//                //在分线程中，实现进度的动态变化
//                new Thread(runnable).start();
//
//
//                banner.setAdapter(new BannerImageAdapter<Image>(images) {
//                    @Override
//                    public void onBindView(BannerImageHolder holder, Image image, int position, int size) {
//                        //图片加载自己实现
//                        Glide.with(holder.itemView)
//                                .load(image.IMAURL)
//                                .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
//                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
//                                .into(holder.imageView);
//                    }
//                });
//                banner.setIndicator(new RectangleIndicator(getContext()));
////                banner.setIndicator(new RoundLinesIndicator(getContext()));
//                banner.setIndicatorSelectedWidth((int) BannerUtils.dp2px(12));
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                //响应失败
//                Toast.makeText(UIUtils.getContext(), "联网获取数据失败", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    @Override
    protected void initTitle() {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("首页");
        ivTitleSetting.setVisibility(View.GONE);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

}
